package com.enbatis.mybatisplugs.controller;

import com.enbatis.mybatisplugs.base.BaseController;
import com.enbatis.mybatisplugs.base.IRedisService;
import com.enbatis.mybatisplugs.commons.sql.Wrapper;
import com.enbatis.mybatisplugs.commons.utils.ResultReturn;
import com.enbatis.mybatisplugs.entity.User;
import com.enbatis.mybatisplugs.entity.vo.SysUserVO;
import com.enbatis.mybatisplugs.plugin.Pages;
import com.enbatis.mybatisplugs.service.MongoDbService;
import com.github.pagehelper.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.enbatis.mybatisplugs.entity.SysUser;
import com.enbatis.mybatisplugs.service.SysUserService;
import org.springframework.web.multipart.MultipartFile;


import java.util.List;

@RestController
@RequestMapping("v1/sys_user")
public class SysUserController extends BaseController {

@Autowired
private SysUserService sysUserService;


    @Autowired
    private IRedisService iRedisService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;




    @ApiOperation(value = "注册用户",notes = "注册用户")
    @PostMapping("/add")
    public ResultReturn register(@RequestBody SysUserVO sysUser){
        sysUser.setCompanyId(getAccount().getCompanyId());
        return  sysUserService.insertUser(sysUser);
    }

    @ApiOperation(value = "注册用户",notes = "注册用户")
    @PostMapping("/register")
    public ResultReturn registerUser(@RequestBody SysUserVO sysUser){
        sysUser.setCompanyId(0L);
        return  sysUserService.insertUser(sysUser);
    }
    @ApiOperation(value = "根据 ID 删除用户",notes = "根据 ID 删除用户")
    @DeleteMapping("/delete")
    public ResultReturn<Integer> delete(@RequestParam("id") Long id){
        return success(sysUserService.deleteById(id));
    }


    @PutMapping("/{id}")
    public  ResultReturn<Integer> update(@PathVariable("id") Long id,@RequestBody SysUser sysUser){
        sysUser.setId(id);
        int count= sysUserService.updateById(sysUser);
        redisTemplate.opsForValue().set("user:"+id,sysUser);
        return success(count);
    }


    @GetMapping("/{id}")
    public ResultReturn<SysUser> get(@PathVariable("id") Long id) {
        return success(sysUserService.getById(id));
//        SysUser user = (SysUser)redisTemplate.opsForValue().get("user:"+id);
//
//        if (null!=user){
//            return success(user);
//        }else{
//            SysUser dbUser=  sysUserService.getById(id);
//            redisTemplate.opsForValue().set("user:"+id,dbUser);
//            return success(sysUserService.getById(id));
//        }

    }


    @GetMapping("/list")
    public ResultReturn<List<SysUser>> getList(SysUser sysUser){
        return success(sysUserService.list(new Wrapper<>(sysUser)));
    }




        @ApiOperation(value = "分页列表", notes = "分页查询数据列表")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "page", value = "请求数据的页码", required = true, paramType = "query", dataType = "int"),
                @ApiImplicitParam(name = "rows", value = "每页条数", required = true, paramType = "query", dataType = "int"),
                @ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"),
                @ApiImplicitParam(name = "sord", value = "排序方式", paramType = "query", dataType = "String"),
        })
        @PostMapping("/page")
        public ResultReturn<Pages<SysUser>> getPage(SysUser user){
            Page<SysUser> page= getPage();
            return success(sysUserService.page(page,new Wrapper<>(user)));
        }


    @PostMapping(value = "/upload")
    public void uploadFile(@RequestParam("businessId") String businessId, @RequestParam("file") MultipartFile file) throws Exception  {
        sysUserService.uploadHeadPhoto(businessId,file);
    }


    @GetMapping(value = "/preview")
    public ResultReturn<String> preview(@RequestParam("id") Long id){
     return   success(sysUserService.previewHeadPhoto(id)) ;
    }



    @ApiOperation(value = "分页列表", notes = "分页查询数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求数据的页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "rows", value = "每页条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sord", value = "排序方式", paramType = "query", dataType = "String"),
    })
    @PostMapping("/page_vo")
    public ResultReturn<Pages<SysUserVO>> getPageVO(@RequestBody SysUserVO userVO){
        Page<SysUserVO> page= getPage();
        return success(sysUserService.pageVO(page,userVO));
    }


    @PostMapping(value = "/app_login")
    public ResultReturn appLogin(@RequestParam("username") String username, @RequestParam("password")String password)   {
        return sysUserService.appLogin(username,password);
    }





}
